/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package controller

import (
	"did-service/auth"
	"did-service/bean/param"
	"did-service/common/constant"
	"did-service/common/http"
	"strconv"

	"github.com/emicklei/go-restful"
)

func (dc *DidController) ListIssuerPage(req *restful.Request, resp *restful.Response) {

	strPageSize := req.QueryParameter("pageSize")
	strPageNo := req.QueryParameter("pageNo")
	status := req.QueryParameter("status")
	name := req.QueryParameter("name")
	pageSize := 5
	if strPageSize != "" {
		pageSize, _ = strconv.Atoi(strPageSize)
	}
	pageNo := 1
	if strPageNo != "" {
		pageNo, _ = strconv.Atoi(strPageNo)
	}
	page := dc.srv.QueryIssuer(pageSize, pageNo, status, name)
	http.OkWithData(resp, "查询成功", page)
}

func (dc *DidController) GetIssuer(req *restful.Request, resp *restful.Response) {
	id := req.PathParameter("id")
	if id == "" {
		log.Error("id or versionId all empty!")
		http.Failed(resp, "请求参数不合法")
		return
	}
	IssuerResponse, err := dc.srv.GetIssuer(id)
	if err != nil {
		http.Failed(resp, "查询失败")
		return
	}

	http.OkWithData(resp, "查询成功", IssuerResponse)
}

func (dc *DidController) GetCurrentIssuer(req *restful.Request, resp *restful.Response) {
	userType := auth.GetCurrentUserType(req)
	if userType != constant.UserTypeIssuer {
		http.Failed(resp, "用户无权限")
		return
	}
	id := auth.GetCurrentWalletId(req)
	result, err := dc.srv.GetIssuerByWalletId(id)
	if err != nil {
		http.Failed(resp, "查询失败")
		return
	}
	if result == nil {
		http.Failed(resp, "尚未注册发证方")
		return
	}

	http.OkWithData(resp, "查询成功", result)
}

func (dc *DidController) ListIssuerEnum(req *restful.Request, resp *restful.Response) {
	IssuerListEnumResp, err := dc.srv.GetIssuerListEnum()
	if err != nil {
		http.Failed(resp, "查询失败")
		return
	}
	http.OkWithData(resp, "查询成功", IssuerListEnumResp)
}

func (dc *DidController) CreateIssuer(req *restful.Request, resp *restful.Response) {

	var issuerParam param.AuthorityIssuerParam
	if err := req.ReadEntity(&issuerParam); err != nil {
		log.Errorf("parameter types do not match error: %s", err)
		http.Failed(resp, "请求参数不合法")
		return
	}
	issuerParam.WalletId = auth.GetCurrentWalletId(req)
	if err := dc.srv.InsertIssuer(&issuerParam); err != nil {
		log.Errorf("create did document error: %v", err)
		http.Failed(resp, err.Error())
		return
	}
	http.Ok(resp, "注册成功")
}

func (dc *DidController) DeleteIssuer(req *restful.Request, resp *restful.Response) {

	id := req.PathParameter("id")
	if id == "" {
		log.Errorf("parameter wrong!")
		http.Failed(resp, "请求参数不合法")
		return
	}
	if err := dc.srv.DeleteIssuer(id); err != nil {
		log.Errorf("delete id issuer error: %v", err)
		http.Failed(resp, "删除失败")
		return
	}
	http.Ok(resp, "删除成功")
}

func (dc *DidController) UpdateIssuer(req *restful.Request, resp *restful.Response) {
	id := req.PathParameter("id")
	status := req.QueryParameter("status")
	if id == "" {
		log.Error("id all empty!")
		http.Failed(resp, "请求参数不合法")
		return
	}
	if status == "" {
		log.Error("status all empty!")
		http.Failed(resp, "请求参数不合法")
		return
	}
	if err := dc.srv.PutIssuer(id, status); err != nil {
		log.Errorf("update id issuer error: %v", err)
		http.Failed(resp, "审核失败")
		return
	}
	http.Ok(resp, "审核成功")
}
